/*
 * Copyright (C) 2006  Nicolas Normand
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifndef GRAPH_DIRECTED_H_
#define GRAPH_DIRECTED_H_

#include "graph.h"

namespace graph
{
	
template <typename V, typename A>
class directed: public virtual basic<V, A>
{
	public:
	// Constructors & destructor
	directed() {}
	directed(int n, const V initializator):basic<V, A>::basic(n, initializator) {}
	virtual ~directed() {}

	// simple tests
	virtual bool edge_exist(const std::pair<int, int>& edge_)
	{
		return this->edges__.find(edge_) != this->edges__.end();
	}

	// Advanced accessors
	virtual A& edge(const std::pair<int, int>& edge_)
	{
		if (this->edges__.find(edge_) != this->edges__.end())
			return this->edges__[edge_];
		else
			throw graph::exception::edge_not_found();
	}

	virtual A& edge(int first, int second)
	{
		return edge(std::pair<int, int>(first, second));
	}

	std::pair<int, int> adjacent_vertexes(A edge)
	{
		//typename directed<V, A>::edge_t it;
		typename std::map<std::pair<int, int>, A, typename basic<V, A>::ltpair_i_i>::iterator it;

		it = this->edges__.begin();
		while (it != this->edges__.end())
		{
			if (it->second == edge)
				return it->first;
			it++;
		}
		throw graph::exception::vertex_not_found();
	}

	// Add operators
	virtual void new_edge(int first, int second, const A data)
	{
		if (this->vertex_exist(first) && this->vertex_exist(second))
			this->edges__[std::pair<int, int>(first, second)] = data;
		else
			throw graph::exception::vertex_out_of_range();
	}

	virtual void new_edge(V first, V second, const A data)
	{
		int f;
		int s;

		for (unsigned int i=0; i<this->vertexes__.size(); i++)
		{
			if (this->vertexes__[i] == first)
				f = i;
			if (this->vertexes__[i] == second)
				s = i;
		}

		this->new_edge(f, s, data);
	}

	// Erasing operators
	virtual void delete_edge(int first, int second)
	{
		if (this->vertex_exist(first) && this->vertex_exist(second))
		{
			typename basic<V, A>::edge_t::iterator it = this->edges__.find(std::pair<int, int>(first, second));
			if (it == this->edges__.end())
				throw graph::exception::edge_not_found();
			else
				this->edges__.erase(it);
		}
		else
			throw graph::exception::vertex_out_of_range();
	}
};

} // End of: namespace

#endif

